function DepartAdd_Code(id,parentid,name,headuserid,headusername) {
	var buffer = new StringBuffer();
	var hidden="";
	var title="部门";
	if(id!=0 && parentid==0){hidden="hidden";title="公司";}
	$("#divmsg2 .name").html(title);
	buffer.append('<div id="parentid" val="'+id+'" parentid="'+parentid+'" headuserid="'+headuserid+'" class="crmsheet border-bottom '+hidden+'"><div class="ctitle"><div class="cname">上级部门</div><div class="require">*</div></div><div class="ctext"><select id="txt_parentid"></select></div><div class="carrow"><img src="img/arrow.png"/></div></div>');
	buffer.append('<div class="crmsheet border-bottom"><div class="ctitle"><div id="cname" class="cname">'+title+'名称</div><div class="require">*</div></div><div class="ctext"><input id="txt_name" type="text" placeholder="填写'+title+'名称" value="'+name+'"/></div><div class="carrow"></div></div>');
	buffer.append('<div class="crmsheet border-bottom users" show="1" touserid="'+headuserid+'"><div class="ctitle"><div class="cname">'+title+'负责人</div></div><div class="ctext"><input id="txt_headuserid" type="text" placeholder="选择负责人" value="'+headusername+'" readonly="readonly" touserid="'+headuserid+'"/></div><div class="carrow"><img src="img/arrow.png"/></div></div>');
	if(parentid!=0){
	buffer.append('<div id="deletesheet" class="deletesheet" style="margin-top:80px;"><div class="name" style="width:100%; text-align:center;">删除该部门</div></div>');
	buffer.append('<div class="loading">若部门下含有子部门或员工，则不能删除该部门</div>');
	}
	return buffer.toString();
};

function Init_DepartAdd(){
    var data = {};
	data.act = "Depart_Data";
	data.source=source;
	data.userid=userid;
	data.comid=comid;
	data.sign=GetSign(data.act);
	$("#txt_parentid").html('<option value="0">加载中...</option>');
	mui.getJSON(admin_domain, data, function (json) {
	    if (json.state) {
	    	var isselect="";
	    	var str="";
	    	$.each(json.data.departlist,function(i,info){
	    		if(info.big.ID+""==$("#parentid").attr("parentid"))
	    		{
	    			isselect="selected='selected'";
	    		}
	    		else
	    		{
	    			isselect="";
	    		}
	    		str+='<option value="'+info.big.ID+'" '+isselect+'>'+info.big.Name+'</option>';
	    	});
	    	$("#txt_parentid").html(str);
	    }
	});
	
	
	mui(".divmsg").on("tap",".users",function(){
			obj10=$(this);
			OpenWindow(4,"选择负责人","确定",function(){
					var str1="",str2="",str3="",headimg="";
					touserid="";todepartid="";tocomid="";
	      			var count=0;
	      			mui.each(mui("#userlist input"),function(i,info){
	      				if(info.checked)
	      				{
	      					count++;
	      					touserid += info.value+",";
	      					str1 += info.getAttribute("val")+",";
	      				}
	      			});
	      			if(count>1)
	      			{
	      				plus.nativeUI.toast("只能选择1个负责人");
	      				return;
	      			}
	      			obj10.find("input").val(str1.TrimEnd(","));
	      			obj10.find("input").attr("touserid",touserid.TrimEnd(","));
					obj10.attr("touserid",touserid.TrimEnd(","));
	      			CloseWindow();
			});
			
			$("#contentlist4").html(CheckMate_Data());
			data = {};
			data.act = "DepartAndUser_Data";
			data.userid=userid;
			data.comid=comid;
			data.source=source;
			data.sign=GetSign(data.act);
			
			mui.getJSON(admin_domain, data, function(json) {
				Bind_CheckMate(json.data.user,json.data.depart);
				Init_CheckMate();
				
			});
			if($(this).attr("show")=="1")
	      	{
	      		$(".selectbar").hide();
	      	}else
	      	{
				$(".selectbar").show();  				
	      	}
	    });
		
		if($("#parentid").attr("parentid")!="0")
		{
			get("deletesheet").addEventListener("tap",function(){
		        	plus.nativeUI.confirm("确定要删除吗？", function(event){
						if(event.index==1)
						{
							var data = {};
						    data.act = "Delete_Depart";
						    data.id = $("#parentid").attr("val");
						    data.userid=userid;
							data.comid=comid;
							data.source=source;
							data.sign=GetSign(data.act);
						    mui.getJSON(admin_domain, data, function(json) {
								if(json.state)
								{
									obj.hide(300);
									CloseWindow();
									plus.nativeUI.toast(json.msg);
								}else
								{
									plus.nativeUI.alert(json.msg);
								}
								
						    });	
						}
					},"提示", ["取消","确认"]); 
				
			});
		}
};




function Save_Depart() {
        var data = {};
        var txt_name = $("#txt_name");
        data.id = $("#parentid").attr("val");
        if(data.id==0)
        {
        	data.act = "Add_EDepart";
        }else
        {
        	data.act = "Edit_EDepart";
        }
        if($("#parentid").attr("val")!=0 && $("#parentid").attr("parentid")==0)
        {
        	data.parentid=0;
        }else
        {
        	data.parentid= $("#txt_parentid").val();
        }
        if($("#txt_parentid").val()==0)
        {
        	plus.nativeUI.toast("请选择上级部门");
        	return false;
        }
        if(txt_name.val().Trim()=="")
        {
        	plus.nativeUI.toast("请填写"+$("#cname").html());
        	return false;
        }
        data.name = txt_name.val();
        data.headuserid=$("#txt_headuserid").attr("touserid");
        data.userid=userid;
		data.comid=comid;
        data.source = source;
        data.version = version;
        data.sign=GetSign(data.act);
        mui.getJSON(admin_domain, data, function (json) {
        	plus.nativeUI.toast(json.msg);
            if (json.state) {
                CloseWindow();
                Receive_Depart(1);
                if(data.parentid==0 && User.TopDepartID==0)
                {
                	User.DepartName=data.name;
                }
                
            } else {
				plus.nativeUI.toast(json.msg); 
            }
        });
};
